<!--综合业务-入户安检预约-->
<template>
  <div class="security-check-box">
    <PageTitle class="boxTitle" :text="title"></PageTitle>
    <div class="con-box detailContent">
      <div class="primary-tit title">请如实填写以下信息完成申请</div>
      <div>
        <tui-form ref="form">
          <tui-input label="用户编号" :required="true" :lineLeft="false" placeholder="请输入用户编号(表号)" v-model="formData.userNo"></tui-input>
          <tui-input label="联系人姓名" :required="true" :lineLeft="false" placeholder="请输入联系人姓名" v-model="formData.name"></tui-input>
          <tui-input label="服务地址" :required="true" :lineLeft="false" placeholder="请输入地址" v-model="formData.address"></tui-input>
          <tui-input label="联系电话" :required="true" :lineLeft="false" type="number" placeholder="请输入联系电话" v-model="formData.phone" maxlength="11"></tui-input>

          <!-- 替换为tui-radio-group实现申请人类别选择 -->
          <div class="radio-group-wrap">
            <span class="required-mark" style="color: red;margin-left: 5px;margin-right: 5px;">*</span>
            <span class="radio-label">申请人类别</span>

            <tui-radio-group v-model="formData.applicantType" class="radio-group">
              <div class="radio-item">
                <tui-radio value="1" :checked="formData.applicantType === '1'"></tui-radio>
                <span class="radio-text">房主</span>
              </div>
              <div class="radio-item">
                <tui-radio value="2" :checked="formData.applicantType === '2'"></tui-radio>
                <span class="radio-text">非房主(实际居住人)</span>
              </div>
            </tui-radio-group>
          </div>

          <!-- 非房主时显示实际居住人电话 -->
          <tui-input
              v-if="formData.applicantType === '2'"
              label="实际居住人电话"
              :required="true"
              :lineLeft="false"
              type="number"
              placeholder="请输入实际居住人电话"
              v-model="formData.nonOwnerPhone"
              maxlength="11"
          ></tui-input>

          <view class="letterType">
            <view class="itemTit">您期望的上门时间</view>
            <lb-picker ref="picker1" mode="multiSelector" :list="dateList" :level="2" :columnNum="10" @confirm="pickerConfirm">
              <input class="tui-input" :required="true" :value="formData.time" placeholder-class="tui-phcolor" disabled name="cause" placeholder="请选时间" maxlength="50" type="text" />
            </lb-picker>
            <tui-icon name="arrowright" size="22" color="#ccc"></tui-icon>
          </view>
          <tui-textarea label="备注信息" :borderTop="false" placeholder="请输入备注信息" v-model="formData.remarks"></tui-textarea>
          <view class="tui-btn__box">
            <tui-button height="84rpx" @click="submit" :disabled="loading" :loading="loading">提交</tui-button>
          </view>
        </tui-form>
      </div>
    </div>
    <div class="con-box tips">
      <div class="warning-tit title">温馨提示</div>
      <div>
        <p class="p">1、按照国家相关法律及法规标准规定，我公司安排专业人员分别为居民用户每2年和非居民用户每1年，提供不少于一次免费安全检查，同时开展安全用气的宣传。请您积极配合我公司的户内安全检查工作，配合在我司安检系统中签字并签收《管道燃气安全使用须知》，如有隐患需签收《户内安全检查隐患整改通知单》并对照燃气安全隐患及时整改。</p>
        <p class="p">2、如用户不在家需要提供该项服务可拨打95007或在本微信公众号预约安检模块预约安检，工作人员将在收到预约后尽快与您联系。</p>
        <p class="p">3、注意事项</p>
        <p class="p">1）公司对经营范围内的用户在一个安检周期内提供免费安全检查，不收取任何费用，更换燃气具、软管、燃气设施设备需用户自费进行更换;上门服务时，我公司工作人员均会佩戴工作牌。如有其它公司或外来人员冒充安检员上门，售卖燃气相关设备，请注意甄别是否为重庆中燃城市燃气发展有限公司工作人员。</p>
        <p class="p">2）如安检时用户家中未留人，安检人员会在门上张贴《到访不遇》单，请用户根据到访不遇留言上的电话与安检人员联系预约下次上门安检时间。</p>
      </div>
    </div>

    <FooterTel></FooterTel>
  </div>
</template>

<script>
import PageTitle from '../components/pageTitle.vue'
import FooterTel from '../components/footerTel.vue'
import LbPicker from '@/components/lb-picker'
import moment from 'moment'
const rules = [
  {
    name: 'userNo',
    rule: ['required'],
    msg: ['请输入用户编号']
  },
  {
    name: 'name',
    rule: ['required'],
    msg: ['请输入联系人姓名']
  },
  {
    name: 'address',
    rule: ['required'],
    msg: ['请输入地址']
  },
  {
    name: 'applicantType',
    rule: ['required'],
    msg: ['请输入申请人类别']
  },
  {
    name: 'phone',
    rule: ['required', 'isMobile'],
    msg: ['请输入手机号', '请输入正确的手机号']
  },
  {
    name: 'time',
    rule: ['required'],
    msg: ['请选择时间']
  },
  // 非房主时验证实际居住人电话
  {
    name: 'nonOwnerPhone',
    rule: [
      (v, data) => data.applicantType !== '2' ? true : (v && v.trim() !== ''),
      (v, data) => data.applicantType !== '2' ? true : /^1[3-9]\d{9}$/.test(v)
    ],
    msg: ['请输入实际居住人电话', '请输入正确的手机号']
  }
]
export default {
  name: 'Detail',
  components: {
    PageTitle,
    FooterTel,
    LbPicker
  },
  data() {
    return {
      title: null,
      formData: {
        userNo: '',
        openid: '',
        name: '',
        address: '',
        phone: '',
        startTime: '',
        endTime: '',
        remarks: '',
        time: '',
        // 新增字段
        applicantType: '', // 1-房主，2-非房主(实际居住人)
        nonOwnerPhone: '' // 非房主时的实际居住人电话
      },
      dateList: [],
      rules: rules,
      loading: false,
      openId: null
    }
  },
  async onShow() {},
  async onLoad(e) {
    var vm = this
    vm.title = e.title
  },
  created() {},
  methods: {
    pickerConfirm(e) {
      var vm = this
      vm.formData.time = e.item[0].label + ' ' + e.item[1].label
      if (e.item[1].value == '1') {
        vm.formData.startTime = e.item[0].value + ' ' + '08:30:00'
        vm.formData.endTime = e.item[0].value + ' ' + '12:00:00'
      }
      if (e.item[1].value == '2') {
        vm.formData.startTime = e.item[0].value + ' ' + '14:00:00'
        vm.formData.endTime = e.item[0].value + ' ' + '20:00:00'
      }
    },
    submit() {
      var vm = this,
          data = {}
      vm.getId()
      if (!vm.$refs.form) return
      vm.formData.openid = vm.openId
      data = JSON.parse(JSON.stringify(vm.formData))
      vm.$delete(data, 'time')
      vm.loading = true
      vm.$refs.form.validate(vm.formData, vm.rules).then(res => {
        vm.tui
            .request('/api/appointment/add', 'post', data, false, false, true)
            .then((res) => {
              if (res.code === 200) {
                uni.showToast({
                  icon: 'success',
                  title: '申请成功'
                })
                setTimeout(() => {
                  vm.formData = {}
                  uni.redirectTo({
                    url: '/pages/index/index'
                  })
                  vm.loading = false
                }, 3000)
              } else {
                uni.showToast({
                  icon: 'error',
                  title: `申请失败，原因：${res.message}`
                })
              }
            })
            .catch((e) => {})
      }).catch(errors => {
        console.log(errors)
        this.loading = false;
      })
    },
    getId() {
      var vm = this
      vm.openId = uni.getStorageSync('openId')
      // 没有openId跳转获取code页面
      if (!vm.openId) return vm.func.goto('/pages/getCode/index')
    }
  },
  mounted() {
    var vm = this
    // 获取当前日期和时间
    let currentDate = moment()
    let endDate = moment().add(30, 'days')
    let dateRange = [] // 存储日期的数组

    while (currentDate.isSameOrBefore(endDate)) {
      dateRange.push(currentDate.format('YYYY-MM-DD'))
      currentDate.add(1, 'days')
    }
    const allHour = [
      { label: '08:30-12:00', value: '1' },
      { label: '14:00-20:00', value: '2' }
    ]
    const pm = [{ label: '14:00-20:00', value: '2' }]

    let list = []

    let now = moment()

    for (let i = 0; i < dateRange.length; i++) {
      let dateStr = dateRange[i]

      let hours = allHour
      // 上午十点，下午四点
      if (dateStr === moment().format('YYYY-MM-DD')) {
        if (now.isSameOrAfter(moment().hour(10).minute(0))) {
          hours = pm
        }
        if (now.isSameOrAfter(moment().hour(16).minute(0))) {
          continue
        }
      }

      list.push({
        label: dateStr,
        value: dateStr,
        children: hours
      })
    }

    vm.dateList = list
  }
}
</script>

<style lang="scss" scoped>
@import '../../static/css/module.scss';
.security-check-box {
  padding-bottom: 20px;
  .boxTitle {
    margin: 10px 14px;
  }
  .detailContent,
  .tips {
    p.p {
      padding: 4px 0;
    }
    padding: 0 18px 20px;
    margin: 14px;
    font-size: 15px;
    line-height: 22px;
    overflow: hidden;
    .title {
      width: 100%;
      height: 30px;
      padding-left: 14px;
      line-height: 30px;
      font-size: 17px;
      font-weight: bold;
      color: #222;
      box-sizing: border-box;
      margin: 10px 0;
    }
    :v-deep .tui-textarea__wrap,
    :v-deep .tui-input__wrap,
    :v-deep .tui-select__wrap {  /* 新增select样式 */
      padding: 12px 0 12px 8px !important;
    }
    :v-deep .tui-placeholder,
    :v-deep .tui-textarea__label,
    :v-deep .tui-textarea__self,
    :v-deep .uni-input-placeholder,
    :v-deep .tui-input__label,
    :v-deep.tui-input__self,
    :v-deep .tui-select__label {  /* 新增select标签样式 */
      font-size: 15px !important;
      uni-text,
      text {
        font-size: 15px !important;
      }
    }
    :v-deep.tui-textarea__wrap {
      align-items: flex-start !important;
    }
    :v-deep .tui-input__required,
    :v-deep .tui-select__required {  /* 新增select必填标识样式 */
      left: 0;
      top: 45%;
    }
    .tui-btn__box {
      padding-top: 30px;
    }
  }
  :v-deep .tui-form__errmsg {
    left: 14px;
    right: 14px;
  }

  .letterType {
    display: flex;
    align-items: baseline;
    padding: 12px 0 12px 8px;
    border-bottom: 1px solid #f2f2f2;
    .itemTit {
      min-width: 77px;
      padding-right: 6px;
      font-size: 15px;
      color: #333;
      position: relative;
      &::before {
        position: absolute;
        content: '*';
        left: -8px;
        top: -2px;
        color: #eb0909;
      }
    }
    .lb-picker {
      width: 100%;
      z-index: 1;
      .tui-input {
        width: 70%;
      }
    }
    :v-deep .uni-input-input {
      font-size: 15px;
    }
    .tui-icon {
      position: absolute;
      right: 0px;
      z-index: 0;
    }
  }
}
</style>